﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using AppGPS.ViewModels;
using System.Threading.Tasks;
using Models;
using AppGPS.Helper;

namespace AppGPS.Views.UControl
{
    public partial class CreateAccount : UserControl
    {
        UserRequest _userRequest = new UserRequest(App.dataShare.szHost);

        private UserResult _userdata = new UserResult();

        public UserResult Data
        {
            get {
                _userdata.Username = Username;
                _userdata.Password = Password;
                _userdata.Online= false;
                _userdata.Status = 1;
                return _userdata; 
            }
        }
        public CreateAccount()
        {
            InitializeComponent();
        }
        public string Username
        {
            get { return txtUsername.Text; }
            set { txtUsername.Text = value; }
        }
        public string Password
        {
            get { return txtPassword.Password; }
        }
        public bool ComparePassword()
        {
            if (txtPassword.Password == txtRePassword.Password)
                return true;
            else
                return false;
        }
        public async Task<bool> CompareUsername()
        {
            return await _userRequest.UsernameExists(txtUsername.Text);
        }
        public async Task<UserResult> CreateTask(UserResult data)
        {
            return await _userRequest.CreateAccount(data);
        }
        private void GotFocus_InputBox(object sender, RoutedEventArgs e)
        {
            if(sender is TextBox)
            {
                var ObjectContext = sender as TextBox;

                if (ObjectContext.Text == "Nhập tài khoản")

                    ObjectContext.Text = "";
            }
            if (sender is PasswordBox)
            {
                var ObjectContext = sender as PasswordBox;

                if (ObjectContext.Password == "Mật khẩu")
                {
                    ObjectContext.Password = "";
                }
            }
        }
        private void LostFocus_InputBox(object sender, RoutedEventArgs e)
        {
            if(sender is TextBox)
            {
                var ObjectContext = sender as TextBox;

                if (ObjectContext.Text == "")

                    ObjectContext.Text = "Nhập tài khoản";
            }
            if (sender is PasswordBox)
            {
                var ObjectContext = sender as PasswordBox;

                if (ObjectContext.Password == "")
                {
                    ObjectContext.Password = "Mật khẩu";
                }
            }
        }
    }
}
